<!DOCTYPE html>
<html lang="en">
<head>
    {{include file="lib/page_head" /}}

    <style>
        table th{word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        table td{word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        .from_table .td_l,.from_table .td_r{font-size:14px!important;line-height: 25px;word-wrap:break-word;word-break:break-all}
        #search_form .form-control,#search_form .select5{width:100px;height:28px;padding:5px;}
        #search_form .layui-input-inline{width:auto;margin: 0;}
        #search_form .form-group{margin-right:0;margin-bottom: 5px;}
        #search_form .btn{height:30px;line-height:30px;padding:0 10px;}
    </style>
</head>

<body>



<!-- Preloader -->
<div id="preloader">
    <div id="status"><i class="fa fa-spinner fa-spin"></i></div>
</div>

<section>
    {{include file="lib/page_left" /}}
    <div class="mainpanel">

        {{include file="lib/page_header" /}}

        <div class="contentpanel" >
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-btns">
                        <a href="" class="panel-close">×</a>
                        <a href="" class="minimize">−</a>
                    </div>
                    <h4 class="panel-title">用户卡列表</h4>
                </div>
                <div class="panel-body ">
                    <div id="info_list" class="form-inline">
                        <div class="btn-group">
                            <button class="btn btn-primary" onclick="bind_card()">绑定老卡</button>
                            <button class="btn btn-primary" onclick="bind_new_card()">绑定新卡</button>
                            <!--<button class="btn btn-primary" onclick="del_info(0)">删除</button>-->
                            <!--<button class="btn btn-primary" onclick="set_cancel(0)">取消</button>-->
                            <button class="btn btn-primary" onclick="location.href = location.href;">刷新</button>
                        </div>
                        <div class="layui-form-item search_tools" style="margin-top:10px;" >
                            <form class="layui-form" id="search_form" action="" onsubmit="return false;">
                                <div  class="form-group">
                                    <label >用户</label>
                                    <select class="select5" name="user_id" data-search="true" >
                                        <option value="0">请选择</option>
                                        {{foreach $user_ids as $val }}
                                        <option value="{{$val.id}}">{{$val.mobile}}</option>
                                        {{/foreach}}
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label >加油卡号</label>
                                    <input class="form-control" name="card_no"  autocomplete="off" type="text">
                                </div>
                                <div style="display: inline-block;clear: both;">
                                    <button class="btn btn-primary" onclick="search()">查询</button>
                                    <button class="btn btn-default" onclick="search_reset()">取消</button>
                                </div>
                            </form>
                            <div class="well well-sm search_toogle"><span class="glyphicon glyphicon-chevron-down"></span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!-- contentpanel -->

    </div><!-- mainpanel -->
    {{include file="lib/page_right" /}}
</section>
<div id="edit_info" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">
        <form>
            <input type="hidden" name="id" value="0">
            <div  class="form-group">
                <label >用户<span style="color:red;">*</span></label>
                <select class="select5" name="user_id" data-search="true" >
                    <option value="0">请选择</option>
                    {{foreach $user_ids as $val }}
                    <option value="{{$val.id}}">{{$val.mobile}}</option>
                    {{/foreach}}
                </select>
            </div>
            <div  class="form-group">
                <label >卡号<span style="color:red;">*</span>(<a href="">选择</a> )</label>
                <input class="form-control" placeholder="卡号" name="card_no" type="text">
            </div>
            <div  class="form-group">
                <label > 卡<span style="color:red;">*</span></label>
                <select class="select5" name="card_type">
                    <option value="0">老卡</option>
                    <option value="1" selected="selected">新卡</option>
                </select>
            </div>

            <div  class="form-group">
                <label > 状态<span style="color:red;">*</span></label>
                <select class="select5" name="status">
                    <option value="0">申请中</option>
                    <option value="1">正常</option>
                    <option value="2">禁用</option>
                </select>
            </div>
        </form>
    </div>
</div>
<div id="bind_card" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">
        <form>
            <input type="hidden" name="id" value="0">
            <div  class="form-group">
                <label >用户<span style="color:red;">*</span></label>
                <select class="select5" name="user_id">
                    <option value="0">请选择</option>
                    {{foreach $user_ids as $val }}
                    <option value="{{$val.id}}">{{$val.real_name}}</option>
                    {{/foreach}}
                </select>
            </div>
            <div  class="form-group">
                <label >卡号<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="卡号" name="card_no" type="text">
            </div>
        </form>
    </div>
</div>
<div id="bind_new_card" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">
        <form>
            <input type="hidden" name="id" value="0">
            <div  class="form-group">
                <label >用户<span style="color:red;">*</span></label>
                <select class="select5" name="user_id">
                    <option value="0">请选择</option>
                    {{foreach $user_ids as $val }}
                    <option value="{{$val.id}}">{{$val.real_name}}</option>
                    {{/foreach}}
                </select>
            </div>
            <div  class="form-group">
                <label >卡号<span style="color:red;">*</span></label>
                <select class="select5" name="card_no" data-search="true">
                    <option value="0">请选择</option>
                    {{foreach $user_card_idle_list as $val }}
                    <option value="{{$val.card_no}}">{{$val.card_no}}</option>
                    {{/foreach}}
                </select>
            </div>
        </form>
    </div>
</div>
<div id="card_refund" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">
        <form>
            <div class="form-group">
                <label>类型<span style="color:red;">*</span></label>
                <div class="form-control">
                    <input type="radio" name="mode" value="1" title="用户退卡" >
                    <input type="radio" name="mode" value="0" title="代理退卡" checked="">
                </div>
            </div>
            <div  class="form-group">
                <label >卡号<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="卡号" name="card_no" type="text" disabled>
            </div>
            <div  class="form-group">
                <label >退还金额<span style="color:red;">*(包含卡费：元)</span></label>
                <input class="form-control" placeholder="退还金额" name="money" value="0" type="text" >
            </div>
            <div  class="form-group">
                <label >代理退还佣金<span style="color:red;">*(元)</span></label>
                <input class="form-control" placeholder="代理退还佣金" name="refund_rebate" value="0" type="text" >
            </div>
        </form>
    </div>
</div>
<div id="card_change" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">
        <form>

            <div  class="form-group">
                <label >老卡号<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="卡号" name="old_card_no" type="text" disabled>
            </div>
            <div  class="form-group">
                <label >卡号<span style="color:red;">*</span></label>
                <select class="select5" name="card_no" data-search="true">
                    <option value="">请选择</option>
                    {{foreach $idle_card_list as $val }}
                    <option value="{{$val.card_no}}">{{$val.card_no}}</option>
                    {{/foreach}}
                </select>
            </div>
            <div  class="form-group">
                <label > 运费</label>
                <input class="form-control" placeholder="运费" name="freight" type="number" value="" data-ignore="true">
            </div>
            <div  class="form-group">
                <label > 卡费</label>

                <input class="form-control" placeholder="卡费" name="card_fee" type="number" value="" data-ignore="true">
            </div>

        </form>
    </div>
</div>
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/jquery-ui-1.10.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jquery.sparkline.min.js"></script>
<script src="../js/toggles.min.js"></script>
<script src="../js/jquery.cookies.js"></script>
<script src="../js/jquery.tmpl.js"></script>
<script src="../js/custom.js"></script>
<script src="../layui/layui.js"></script>
<script src="../js/jquery.gritter.min.js"></script>
<script src="../js/form.beautify.js?t={{$version}}"></script>
<script src="../js/commen.js?t={{$version}}"></script>
<script src="../js/datatables.js?t={{$version}}"></script>
<link href="../css/ley.dialog.css" rel="stylesheet">
<script src="../js/ley.dialog.js?t={{$version}}"></script>
<script>
    layui.use(['laydate','laypage',"element"], function(){

    });
    var admin_id = "{{$ADMIN_ID}}";

    $(function () {


        $("#info_list").datatables({
            idField: 'order_no',
            mobile_field:["ck","mobile","card_no"],
            columns:[
                {field:'ck',checkbox:true },
                {field:'id',title:'编号',width:50},
                {field:'agent_name',title:'代理人',width:50},
                {field:'mobile',title:'手机号',width:100},
                {field:'real_name',title:'真实姓名',width:100},
                {field:'card_no',title:'卡号',width:100},
                {field:'card_type',title:'卡类型',width:50,formatter:function (value,row,index) {
                    if(value===1){
                        return "副卡";

                    }else{
                        return "普通卡";
                    }
                }},
                {field:'status',title:'状态',width:30,formatter:function (value,row,index) {
                    if(value===1){
                        return "正常";
                    }else if(value===2){
                        return "禁用";
                    }else{
                        return "申请中";
                    }
                }},
                {field:'operation',title:'操作',width:30,formatter:function (value,row,index) {
                    var text = "";
                    text += '<div class="btn-group"> ';
                    text += '<a data-toggle="dropdown" class="dropdown-toggle"> ';
                    text += '<i class="fa fa-cog"></i> ';
                    text += '</a> ';
                    text += '<ul role="menu" class="dropdown-menu pull-right"> ';

                    if(row.card_type != 0)
                    {
                        text +='<li><a href="javascript:void 0;" onclick="unbind_card(\''+row.id+'\')">解绑新卡</a></li> ' ;
                    }else{
                        text +='<li><a href="javascript:void 0;" onclick="unbind_card(\''+row.id+'\')">解绑老卡</a></li> ' ;
                    }
                    text +='<li><a href="javascript:void 0;" onclick="card_refund(\''+row.card_no+'\')">退卡</a></li> ' ;
                    text +='<li><a href="javascript:void 0;" onclick="card_change(\''+row.card_no+'\')">换卡</a></li> ' ;
                    text +='</ul> ';
                    text +='</div>';
                    return text;
                }}

            ],
            ajax:{
                url:"/users/ajax_user_card_list",
                parems:get_search_data(),
                callback:function (msg) {
                    check_table_list_data(msg);
                    check_parent_height();
                    if(msg.user_card_idle_list)
                    {
                        var options = "";
                        for(var i=0;i<msg.user_card_idle_list.length;i++)
                        {
                            var idle = msg.user_card_idle_list[i];
                            options += '<option value="'+idle.card_no+'">'+idle.card_no+'</option>';

                        }
                        $("#bind_new_card select[name='card_no']").html(options);
                    }
                }
            }

        });
    });
    function bind_card()
    {
        openDialog2({title:"绑定老卡",area: ['400', "400"],content:$("#bind_card"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type='text'],input[type='hidden'],select,textarea,input[type='radio']:checked,input[type='checkbox']:checked",layero).each(function () {
                    var name = $(this).attr("name");

                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            $(this).parents(".form-group").addClass("has-error");
                            is_false = true;
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/users/ajax_user_card_bind_card",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function bind_new_card()
    {
        openDialog2({title:"绑定新卡",area: ['400', "400"],content:$("#bind_new_card"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type='text'],input[type='hidden'],select,textarea,input[type='radio']:checked,input[type='checkbox']:checked",layero).each(function () {
                    var name = $(this).attr("name");

                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            $(this).parents(".form-group").addClass("has-error");
                            is_false = true;
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/users/ajax_user_card_bind_new_card",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function unbind_card(id)
    {
        promptMsg("是否确定解绑",function () {
            AjaxCommen("GET","/users/ajax_user_card_unbind_card",{id:id},function (msg) {
                if(msg.error === 0)
                {
                    openMsg("成功");
                    search();
                }
            });
        })
    }
    function card_refund(card_no)
    {

        openDialog2({title:"退卡",area: ['400', "420"],content:$("#card_refund"),clone:true,before:function (layero) {
                $("#card_refund input[name='card_no']",layero).val(card_no);
            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type='text'],input[type='hidden'],select,textarea,input[type='radio']:checked,input[type='checkbox']:checked",layero).each(function () {
                    var name = $(this).attr("name");

                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{

                            $(this).parents(".form-group").addClass("has-error");
                            is_false = true;
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/users/ajax_card_refund",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function card_change(card_no)
    {

        openDialog2({title:"换卡",area: ['400', "420"],content:$("#card_change"),clone:true,before:function (layero) {
                $("#card_change input[name='old_card_no']",layero).val(card_no);
            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type='text'],input[type='number'],input[type='hidden'],select,textarea,input[type='radio']:checked,input[type='checkbox']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();
                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{

                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/users/ajax_card_change",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }

    function search()
    {
        var data = get_search_data();
        $("#info_list").setParems(data);
        $("#info_list").reload();

    }
    function search_reset() {
        $("#search_form")[0].reset();
        search();
    }
    function get_search_data() {
        var data = {};
        $('#search_form select,#search_form input').each(function () {
            var name = $(this).attr("name");
            if(name)
            {
                data[name] = $(this).val();
            }

        });
        return data;
    }
</script>

</body>
</html>
